import { Directive } from "../type";
export default {
  name: "debounce",
  install: {
    created(el: Element, binding: Directive.Binding) {
      // 下面会介绍各个参数的细节
      const times = binding.value || 2000;
      let timer: any = null;
      el.addEventListener(
        "click",
        function () {
          if (timer) {
            clearTimeout(timer);
            timer = setTimeout(() => {
              timer = null;
            }, times);
            event && event.stopImmediatePropagation();
          } else {
            timer = setTimeout(() => {
              timer = null;
            }, times);
          }
        },
        true
      );
    }
  }
};
